import java.util.*;

public class PaperList {
    private final String teacher;
    private final List<Paper> papers;
    private final PaperSelection selections;
    private SelectionStrategy strategy = new RandomSelectionStrategy();

    public PaperList(String teacher, List<Paper> papers) {
        this.teacher = teacher;
        this.papers = papers;
        this.selections = new PaperSelection();
    }

    public List<Paper> selectPaperByRandom(Student student) throws NoEnoughPapersException {
        List<Paper> availablePapers = getUnselectedPapersByType(student.getPreferredPaperType());
        int expected = student.getPreferredQuantity();
        int available = availablePapers.size();

        if (available < expected) {
            throw new NoEnoughPapersException(student.getName(), expected, available);
        }

        List<Paper> selected = strategy.selectPapers(availablePapers, expected);
        for (Paper p : selected) {
            selections.addSelection(student, p);
        }
        return selected;
    }

    private List<Paper> getUnselectedPapersByType(PaperType type) {
        List<Paper> result = new ArrayList<>();
        for (Paper paper : papers) {
            if (!selections.isSelected(paper) && (type == PaperType.Any ||
                    (paper instanceof Thesis && type == PaperType.Thesis) ||
                    (paper instanceof JournalPaper && type == PaperType.JournalPaper) ||
                    (paper instanceof ConferencePaper && type == PaperType.ConferencePaper))) {
                result.add(paper);
            }
        }
        return result;
    }

    public PaperSelection getSelectionResult() {
        return selections;
    }

    public void setSelectionStrategy(SelectionStrategy strategy) {
        this.strategy = strategy;
    }

    public List<Paper> getPapers() {
        return new ArrayList<>(papers);
    }
}    